package vn.conghuy.talkwithme.data.livescore;

import java.io.IOException;
import java.util.ArrayList;

import org.jsoup.Jsoup;

public class LivescoreMatch {
	
	private static final String START_TIME = "<td width=\"20\">";
	private static final String START_TIME_PENANTY = "<td width=\"20\"><";
	private static final String START_SCORE_ROW = "<td width=\"50\" align=\"center\"><b>[";
	private static final String START_HOME_PLAYER = "<td width=\"163\" align=\"left\">";
	private static final String START_AWAY_PLAYER = "<td width=\"163\">";
	
	
	public String startTime;
	public String home;
	public String away;
	public int homeScore;
	public int awayScore;
	public String link;
	public ArrayList<LivescoreEvent> events;
	
	public LivescoreMatch(){
		events = new ArrayList<LivescoreEvent>();
	};
	
	public LivescoreMatch(String home, String away, int homeScore,
			int awayScore, String link) {
		super();
		this.home = home;
		this.away = away;
		this.homeScore = homeScore;
		this.awayScore = awayScore;
		this.link = link;
		events = new ArrayList<LivescoreEvent>();
	}
	boolean updateEvent(){
		if(link == null) return false;
		
		try{
			String html = Jsoup.connect(link).userAgent("Mozilla/6.0 (Windows NT 6.2; WOW64; rv:16.0.1) Gecko/20121011 Firefox/16.0.1").get().html();
			String[] lines = html.split("\n");
			LivescoreEvent currentEvent = null;
			for(String line : lines){
				line = line.trim();
				if(line.startsWith(START_TIME) && line.startsWith(START_TIME_PENANTY) == false){
					currentEvent = new LivescoreEvent(this);
					String timeStr = line.substring(line.indexOf(">"));
					timeStr = timeStr.replaceAll(">|'</td>", "");
					currentEvent.time = Integer.valueOf(timeStr);
					events.add(currentEvent);
				}
				else if(line.startsWith(START_SCORE_ROW)){
					String scoreStr = line.substring(line.indexOf(">["));
					scoreStr = scoreStr.replaceAll(">\\[|\\]</b></td>", "");
					String[]scoreElem = scoreStr.split(" - ");
					currentEvent.currentHomeScore = Integer.valueOf(scoreElem[0]);
					currentEvent.currentAwayScore = Integer.valueOf(scoreElem[1]);
					
				}
				else if(line.startsWith(START_HOME_PLAYER)){
					String homePlayer = line.substring(line.indexOf(">"));
					int imageIndex = homePlayer.indexOf("<img");
					if(imageIndex >= 0){
						homePlayer = homePlayer.substring(1, imageIndex);
						currentEvent.homePlayer = Jsoup.parse(homePlayer).text();
						currentEvent.homeType = whichType(line);
					}
				}
				else if(line.startsWith(START_AWAY_PLAYER)){
					String awayPlayer = line.substring(line.indexOf(">"));
					int imageIndex = awayPlayer.indexOf("<img");
					if(imageIndex >= 0){
						awayPlayer = awayPlayer.substring(1, imageIndex);
						currentEvent.awayPlayer = Jsoup.parse(awayPlayer).text();
						currentEvent.awayType = whichType(line);
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return true;
	}
	
	private int whichType(String line) {
		int type = LivescoreEvent.ET_GOAL;
		if (line.contains("nogoal.gif")) type = LivescoreEvent.ET_NO_GOAL;
		else if(line.contains("yellow.gif")) type = LivescoreEvent.ET_YELLOW;
		else if(line.contains("goal.gif"))type = LivescoreEvent.ET_GOAL;
		else if(line.contains("red.gif")) type = LivescoreEvent.ET_RED;
		return type;
	}
	
	@Override
	public String toString() {
		StringBuilder strBuilder = new StringBuilder();
		strBuilder.append(startTime);
		strBuilder.append(" : ");
		strBuilder.append(home);
		strBuilder.append(" ");
		strBuilder.append(homeScore);
		strBuilder.append(" - ");
		strBuilder.append(awayScore);
		strBuilder.append(" ");
		strBuilder.append(away);
		return strBuilder.toString();
	}
	
	public String toStringDetail(){
		StringBuilder strBuilder = new StringBuilder();
		strBuilder.append(toString());
		if(events.size() <= 0) return strBuilder.toString();
		strBuilder.append(" :\n\n");
		for(LivescoreEvent event: events){
			strBuilder.append("- ").append(event.toString()).append("\n");
		}
		return strBuilder.toString();
	}
}
